//
//  XMCollectionController.m
//  XunMi
//
//  Created by MacBook on 2024/2/18.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMCollectionController.h"

@interface XMCollectionController ()

@end

@implementation XMCollectionController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(kNavBarAndStatusBarHeight, 0, 0, 0));
    }];
    
    [self.collectionView.panGestureRecognizer requireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
}

- (void)registerCollectionNibWithNibName:(NSString *)nibName {
    [self.collectionView registerNib:[UINib nibWithNibName:nibName bundle:nil] forCellWithReuseIdentifier:nibName];
}

- (void)registerCollectionClassWithClassName:(NSString *)className {
    [self.collectionView registerClass:NSClassFromString(className) forCellWithReuseIdentifier:className];
}

- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths {
    [UIView setAnimationsEnabled:NO];
    [UIView animateWithDuration:0
                     animations:^{
                         if (@available(iOS 11.0, *)) {
                             [self.collectionView
                                 performBatchUpdates:^{
                                     [self.collectionView reloadItemsAtIndexPaths:indexPaths];
                                 }
                                 completion:^(BOOL finished) {
                                     [UIView setAnimationsEnabled:YES];
                                 }];
                         } else {
                             [self.collectionView reloadItemsAtIndexPaths:indexPaths];
                         }
                     }];
}

- (void)reloadSections:(NSIndexSet *)sections {
    [UIView setAnimationsEnabled:NO];
    [UIView animateWithDuration:0
                     animations:^{
                         if (@available(iOS 11.0, *)) {
                             [self.collectionView
                                 performBatchUpdates:^{
                                     [self.collectionView reloadSections:sections];
                                 }
                                 completion:^(BOOL finished) {
                                     [UIView setAnimationsEnabled:YES];
                                 }];
                         } else {
                             [self.collectionView reloadSections:sections];
                         }
                     }];
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    return cell;
}

#pragma mark - XMCollectionFlowLayoutD
// 一行显示N个
- (NSInteger)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout numberOfColumnInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout itemWidth:(CGFloat)width heightForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGFLOAT_MIN;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout referenceHeightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(XMCollectionFlowLayout *)layout referenceHeightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

#pragma mark - UICollectionView
- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = [UIColor whiteColor];

        [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionReusableView"];
        [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionReusableView"];
        if (@available(iOS 11.0, *)) {
            collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _collectionView = collectionView;
    }
    return _collectionView;
}

- (XMCollectionFlowLayout *)layout {
    if (_layout == nil) {
        _layout = [[XMCollectionFlowLayout alloc] init];
        _layout.dataSource = self;
    }
    return _layout;
}

@end
